﻿using Code;
using github.heerheer.card2.UI.Pages;
using github.heerheer.card2.UI.ViewModels;
using HeerCards;
using Microsoft.Win32;
using Panuon.UI.Silver;
using PropertyChanged;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace github.heerheer.card2.UI.Views
{
    /// <summary>
    /// PoolContentView.xaml 的交互逻辑
    /// </summary>
    public partial class PoolContentView : Window
    {
        public PoolContentViewModel PoolContentViewModel { get; set; }

        public PoolContentView(string pool)
        {
            InitializeComponent();
            this.DataContext = PoolContentViewModel = new PoolContentViewModel();
            this.PoolContentViewModel.LoadPoolContent(pool);
        }

        private void Btn_LevelContent(object sender, RoutedEventArgs e)
        {
            string tag = ((Button)sender).Tag.ToString();
            //var l = PoolContentViewModel.Pool.LevelCollections.Find(s => s.Name == tag);
            var c = new CardEditorView(PoolContentViewModel.Pool.PoolName, tag);
            var b = c.ShowDialog();
            if (b == true)
            {
                this.PoolContentViewModel.LoadPoolContent(PoolContentViewModel.Pool.PoolName);
            }
        }

        private void Btn_Card(object sender, RoutedEventArgs e)
        {
        }

        private void Btn_SavePool(object sender, RoutedEventArgs e)
        {
            //重新给Poollevelcollection
            var li = new List<OBLevel>(PoolContentViewModel.PLevelCollections);

            li.ForEach(s => s.Cards = new List<HCCard>(s.OBCards));
            PoolContentViewModel.Pool.LevelCollections = new List<HCLevel>(li);
            App.SavePool(PoolContentViewModel.Pool);
            MessageBoxX.Show("卡池保存成功了！", "保存成功");
        }

        private void Btn_RemoveLevel(object sender, RoutedEventArgs e)
        {
            if (MessageBoxX.Show("确定要删除卡池?", "注意",this,MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                OBLevel level = (OBLevel)((Button)sender).Tag;
                PoolContentViewModel.PLevelCollections.Remove(level);
            }
        }

        private void Btn_AddLevel(object sender, RoutedEventArgs e)
        {
            InputDialog inp = new InputDialog("输入等级名字");
            var result = inp.ShowDialog();
            if (result == true && inp.Value != string.Empty)
            {
                if (new List<HCLevel>(PoolContentViewModel.PLevelCollections).Find((HCLevel level) => level.Name == inp.Value) == null)
                {
                    PoolContentViewModel.Pool.AddLevel(new HCLevel(0) { Name = inp.Value });
                    App.SavePool(PoolContentViewModel.Pool);
                }
                else
                {
                    MessageBoxX.Show("有一个相同名字的等级惹U•ェ•*U", "发现问题", MainWindow.mainWindow);
                }
            }
        }

        private void CardEditBtn_Click(object sender, RoutedEventArgs e)
        {
            var btntag = int.Parse(((Button)sender).Tag.ToString());
            foreach (var item in PoolContentViewModel.PLevelCollections)
            {
                if (item.hasCard(btntag))
                {
                    var c = item.getguidCard(btntag);

                    CardSettings cs = new CardSettings(c);
                    var result = cs.ShowDialog();
                    if (result == true)
                    {
                        var card2 = cs.Card;
                        //卡池做出了更改
                        foreach (var item2 in PoolContentViewModel.PLevelCollections)
                        {
                            if (item2.hasCard(btntag))
                            {
                                item2.replaceCard(btntag, new HCCard()
                                {
                                    Name = card2.Name,
                                    Description = card2.Description,
                                    ExtraProbability = card2.ExtraProbability,
                                    LocalImagePath = card2.LocalImagePath
                                });
                            }
                        }
                    }
                }
            }
        }

        private void CardDeleteBtn_Click(object sender, RoutedEventArgs e)
        {
            var btntag = int.Parse(((Button)sender).Tag.ToString());
            var r = MessageBoxX.Show("确定要删除这张卡?", "删除", this, MessageBoxButton.YesNo);
            if (r == MessageBoxResult.Yes)
            {
                foreach (var item in PoolContentViewModel.PLevelCollections)
                {
                    if (item.hasCard(btntag))
                    {
                        item.removeCard(btntag);
                    }
                }
            }
            this.DataContext = PoolContentViewModel;
        }

        private void Btn_AddCard(object sender, RoutedEventArgs e)
        {
            var l = (OBLevel)((Button)sender).Tag;
            l.OBCards.Add(new HCCard());
        }

        private void Btn_QuickImport(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Title = "选择需要导入成卡牌的图片";
            var path = AppMain.Common.AppDirectory.Replace("app\\github.heerheer.card2", "image\\card2");
            fd.InitialDirectory = path;
            fd.Multiselect = true;
            var r = fd.ShowDialog();
            if (r == true)
            {
                foreach (var item in fd.FileNames)
                {
                    var imagepath = item.Replace(path, "");
                    var name = Path.GetFileNameWithoutExtension(item);
                    var desc = "";

                    var nc = new HCCard()
                    {
                        Name = name,
                        Description = desc,
                        ExtraProbability = 1,
                        LocalImagePath = imagepath
                    };
                    //CardEditorViewModel.Level.addCard(nc);
                }

                //CardList.ItemsSource = null;
                //CardList.ItemsSource = CardEditorViewModel.Level.Cards;
                //MessageBox.Show("导入完成", "快速构建");
            }
        }
    }

    [AddINotifyPropertyChangedInterface]
    public class OBLevel : HCLevel, INotifyPropertyChanged
    {
        public ObservableCollection<HCCard> OBCards { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        public bool hasCard(int guid)
        {
            foreach (var item in OBCards)
            {
                if (item.GUID == guid)
                {
                    return true;
                }
            }
            return false;
        }

        public void removeCard(int guid)
        {
            foreach (var item in OBCards.ToArray())
            {
                if (item.GUID == guid)
                {
                    OBCards.Remove(item);
                }
            }
        }

        public HCCard getguidCard(int guid)
        {
            foreach (var item in OBCards)
            {
                if (item.GUID == guid)
                {
                    return item;
                }
            }
            return null;
        }

        public void replaceCard(int guid, HCCard card)
        {
            var i = 0;
            foreach (var item in OBCards.ToArray())
            {
                if (item.GUID == guid)
                {
                    OBCards[i] = card;
                }
                i++;
            }
        }
    }
}